﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace SignDetection
{
    class Histogram
    {
        public static Bitmap Histo(Bitmap cls_bitmap, int XLENGTH, int YLENGTH)
        {
            Bitmap bitmap = new Bitmap(XLENGTH, YLENGTH);
            //Bitmap bitmap = new Bitmap(XLENGTH, YLENGTH);

            int[,] Hist = new int[XLENGTH,YLENGTH];
            int[] Gray_Hist = new int[256];
            for (int i = 0; i < cls_bitmap.Width; i++)
                for (int j = 0; j < cls_bitmap.Height; j++)
                {
                    Color pixel = cls_bitmap.GetPixel(i, j);
                    Point family = ColorMap.MapColor(pixel);
                    int gray=(int)((pixel.R+pixel.G+pixel.B)/3);
                    Gray_Hist[gray]++;
                    int x = family.X;
                    int y = family.Y;
                    if (y < YLENGTH && x < XLENGTH && y >= 0 && x >= 0)
                        Hist[x, y]++;
                    else
                    {
                        if (x < -5 || (x - XLENGTH) > 5
                        || y < -5 || (y - YLENGTH) > 5)
                            throw new Exception("Impossible!");
                        continue;
                    }
                }
            //找最大值最小值
            int max = Hist[0,0];
            int min = Hist[0,0];
            for (int x = 0; x < XLENGTH; x++)
                for (int y = 0; y < YLENGTH; y++)
                {
                    int his=Hist[x,y];
                    if (max < his)
                        max = his;
                    if (min > his)
                        min = his;
                }

            for (int x = 0; x < XLENGTH; x++)
                for (int y = 0; y < YLENGTH; y++)
                {
                    int Pixel = 0;
                    if(Hist[x,y]>0)
                        Pixel = (int)(255 * Math.Log10(Hist[x,y]) / (Math.Log10(max)));
                    //if (Hist[y * 256 + i] >255)
                    //    bitmap.SetPixel(i, y, Color.FromArgb(255, 255, 255));
                    //else
                    //Pixel = (int)(255 * (Hist[y * XLENGTH + i] -min) / (max - min));
                        bitmap.SetPixel(x, y,Color.FromArgb( Pixel, Pixel,Pixel));
                }

            Point orginal = new Point((int)ColorMap.offsetx, (int)ColorMap.offsety);
            Graphics g = Graphics.FromImage(bitmap);
            g.DrawEllipse(new Pen(Color.White), 0, 0, 3, 3);
            Point blue = ColorMap.MapColor(Color.FromArgb(0, 0, 255));
            Point green = ColorMap.MapColor(Color.FromArgb(0, 255, 0));
            Point red = ColorMap.MapColor(Color.FromArgb(255, 0, 0));
            g.DrawLine(new Pen(Color.Blue), orginal, blue);
            g.DrawLine(new Pen(Color.Green), orginal, green);
            g.DrawLine(new Pen(Color.Red), orginal, red);
            return bitmap;
        }
    }
}
